package com.szh.htmltopdf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szh.htmltopdf.mapper.YearMapper;
import com.szh.htmltopdf.model.YearModel;
import com.szh.htmltopdf.service.YearService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class YearServiceImpl  extends ServiceImpl<YearMapper,YearModel> implements YearService{
    /**todo 为啥把方法写在 controller 层事务不会生效
     * @param userId 
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dbWrite(Long userId){
        YearModel one = this.getOne(Wrappers.<YearModel>lambdaQuery().eq(YearModel::getUserId, userId));
        if (ObjectUtil.isNull(one)){
            this.save(YearModel.builder().userId(userId).old("").build());
        }else {
            this.updateById(YearModel.builder().id(one.getId()).old("更新").build());
        }
        try {
            Thread.sleep(2_00);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
